//
//  MyViewController.m
//  MilitaryDemo
//
//  Created by qianfeng01 on 15-7-4.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "MyViewController.h"
#import "SDImageCache.h"
#import "MyFavoriteViewController.h"
@interface MyViewController ()<UIActionSheetDelegate>

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataInit];
    [self creatTableView];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49) style:UITableViewStyleGrouped];
     self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}
- (void)dataInit {
    self.dataArr =[NSMutableArray arrayWithObjects:@"我的收藏",@"清除缓存",@"联系我们 944319675@qq.com",nil];
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(double)getCachesSize
{
    double sdSize=[[SDImageCache sharedImageCache]getSize];
    NSString *myCachePath=[NSHomeDirectory() stringByAppendingString:@"/Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumetator=[[NSFileManager defaultManager]enumeratorAtPath:myCachePath];
    double mySize=0;
    for (NSString *fileName in enumetator) {
        NSString *filePath=[myCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *dict=[[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
        mySize+=dict.fileSize;
    }
    double totalSize=(mySize+sdSize)/1024/1024;
    return totalSize;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textLabel.text = self.dataArr[indexPath.section];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        NSString *title=[NSString stringWithFormat:@"删除缓存文件:%.6fM",[self getCachesSize]];
        UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles: nil];
        [sheet showInView:self.view];
    }else if(indexPath.section==0)
    {
        MyFavoriteViewController *controller=[[MyFavoriteViewController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.destructiveButtonIndex) {
        //删除(两部分 一个是sdWebImage 的一个是我们自己的)
        //删除sd的
        //清除内存中的图片缓存
        [[SDImageCache sharedImageCache] clearMemory];
        //清除磁盘上的图片缓存
        [[SDImageCache sharedImageCache] clearDisk];
        
        //删除自己的缓存文件夹
        NSString *myCachePath = [NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];
        //删除
        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
